package scu.maqiang.shalegas;

import scu.maqiang.fes.FES3T41;
import scu.maqiang.mesh.Mesh3T4;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class ReadStressInformation {
    public static void main(String[] args) {
        String nodesFile = "wei202H14\\nodes202H14.txt";
        String elementsFile = "wei202H14\\elements202H14.txt";
        String stressFile = "wei202H14\\stresses202H14.txt";
        String pressureFile = "wei202H14\\pressure202H14.txt";
        String fluid0File = "wei202H14\\fluid0202H14.txt";
        String velocityFile = "wei202H14\\velocity202H14.txt";
        String displacemenetFile = "wei202H14\\displacement202H14.txt";
        treatStresInformation(nodesFile, elementsFile, stressFile, pressureFile, fluid0File, velocityFile, displacemenetFile);

//        String nodesFile = "wei202H18\\nodes202H18.txt";
//        String elementsFile = "wei202H18\\elements202H18.txt";
//        String stressFile = "wei202H18\\stresses202H18.txt";
//        treatStresInformation(nodesFile, elementsFile, stressFile);

//        String nodesFile = "wei204H38\\nodes204H38.txt";
//        String elementsFile = "wei204H38\\elements204H38.txt";
//        String stressFile = "wei204H38\\stresses204H38.txt";
//        treatStresInformation(nodesFile, elementsFile, stressFile);
    }

    public static void treatStresInformation(String nodesFile, String elementsFile, String stressFile,
                                             String pressureFile, String fluid0File, String velocityFile, String displcementFile) {
//        //int ele = (int)(Double.parseDouble("1.0e002"));
//        //System.out.println(ele);
        Mesh3T4 mesh3D = new Mesh3T4().readMeshVTU(nodesFile, elementsFile);
        mesh3D.toTecplot("wei202H14\\Mesh202H14.dat");
        Scanner stressScanner = null;
        Scanner pressureScanner = null;
        Scanner fluid0Scanner = null;
        Scanner velScanner = null;
        Scanner dispScanner = null;
        int Nt = mesh3D.getNt();
        int Nv = mesh3D.getNv();
        try {
            stressScanner = new Scanner(new File(stressFile));
            pressureScanner = new Scanner(new File(pressureFile));
            fluid0Scanner = new Scanner(new File(fluid0File));
            velScanner = new Scanner(new File(velocityFile));
            dispScanner = new Scanner(new File(displcementFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
        double[][] stress = new double[9][Nt];
        double[][] velocity = new double[3][Nt];
        for(int i = 0; i < Nt; i++) {
            stress[0][i] = stressScanner.nextDouble();
            stress[1][i] = stressScanner.nextDouble();
            stress[2][i] = stressScanner.nextDouble();
            stress[3][i] = stressScanner.nextDouble();
            stress[4][i] = stressScanner.nextDouble();
            stress[5][i] = stressScanner.nextDouble();
            stress[6][i] = stressScanner.nextDouble();
            stress[7][i] = stressScanner.nextDouble();
            stress[8][i] = stressScanner.nextDouble();
            velocity[0][i] = velScanner.nextDouble();
            velocity[1][i] = velScanner.nextDouble();
            velocity[2][i] = velScanner.nextDouble();
        }

        double[] pressure = new double[Nv];
        double[] fluid0 = new double[Nv];
        double[][] displacement = new double[3][Nv];
        for(int i = 0; i < Nv; i++) {
            pressure[i] = pressureScanner.nextDouble();
            fluid0[i] = fluid0Scanner.nextDouble();
            displacement[0][i] = dispScanner.nextDouble();
            displacement[1][i] = dispScanner.nextDouble();
            displacement[2][i] = dispScanner.nextDouble();
        }
        FES3T41 fs = new FES3T41(mesh3D);
//        double[][] stressXY = new double[9][];
        double[] stressXX = stress[0];
        double[] stressXY = stress[1];
        double[] stressYY = stress[4];
        double[] minHorizontal = new double[Nt];
        double[] maxHorizontal = new double[Nt];
//        for(int i = 0; i < 9; i++) {
//            stressXY[i] = fs.getNodeInfoFromElementInfo(stress[i]);
//        }
        Arrays.setAll(minHorizontal, i -> 0.5 * (stressXX[i] + stressYY[i])
                - Math.sqrt((stressXX[i] - stressYY[i]) * (stressXX[i] - stressYY[i])  + stressXY[i] * stressXY[i]));
        Arrays.setAll(maxHorizontal, i -> 0.5 * (stressXX[i] + stressYY[i])
                + Math.sqrt((stressXX[i] - stressYY[i]) * (stressXX[i] - stressYY[i])  + stressXY[i] * stressXY[i]));
        double[][] stressHorizontal = new double[2][];
        stressHorizontal[0] = fs.getNodeInfoFromElementInfo(minHorizontal);
        stressHorizontal[1] = fs.getNodeInfoFromElementInfo(maxHorizontal);

        double[][] velocityNode = new double[3][];
        velocityNode[0] = fs.getNodeInfoFromElementInfo(velocity[0]);
        velocityNode[1] = fs.getNodeInfoFromElementInfo(velocity[1]);
        velocityNode[2] = fs.getNodeInfoFromElementInfo(velocity[2]);
        mesh3D.toTecplot("wei202H14\\StressHorizontal202H14.dat", stressHorizontal);
        mesh3D.toTecplot("wei202H14\\Fluid0202H14.dat", fluid0);
        mesh3D.toTecplot("wei202H14\\Pressure202H14.dat", pressure);
        mesh3D.toTecplot("wei202H14\\Velocity202H14.dat", velocityNode);
        mesh3D.toTecplot("wei202H14\\Displacement202H14.dat", displacement);
    }
}